package cn.hg.jboot.core.vo;

import com.jfinal.kit.JsonKit;

import cn.hg.jboot.core.constant.BaseConstants;

/**
 * 应答基类
 * 
 * @author 胡高
 * @date 2021/10/11
 */
public abstract class BaseResponseVo<T> extends BaseVo {


    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -5582682415947538664L;

    /**
     * @param <T>
     * @return
     */
    public static <T> BaseResponseVo<T> fail() {
        return BaseResponseVo.fail(BaseConstants.RETURN_CODE_VALUE_FAILED, BaseConstants.RETURN_MESSAGE_VALUE_FAILED, null);
    }

    /**
     * @param <T>
     * @param code
     * @param msg
     * @return
     */
    public static <T> BaseResponseVo<T> fail(Integer code, String msg) {
        return BaseResponseVo.fail(code, msg, null);
    }

    /**
     * @param <T>
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static <T> BaseResponseVo<T> fail(Integer code, String msg, T data) {
        return new BaseResponseVo<T>(false, code, msg, data) {
            /**
             * serialVersionUID
             */
            private static final long serialVersionUID = 1759852948460806820L;
        };
    }

    /**
     * @param <T>
     * @param data
     * @return
     */
    public static <T> BaseResponseVo<T> fail(T data) {
        return BaseResponseVo.fail(BaseConstants.RETURN_CODE_VALUE_FAILED, BaseConstants.RETURN_MESSAGE_VALUE_FAILED, data);
    }

    /**
     * @param <T>
     * @return
     */
    public static <T> BaseResponseVo<T> ok() {
        return BaseResponseVo.ok(BaseConstants.RETURN_CODE_VALUE_SUCCESSED, BaseConstants.RETURN_MESSAGE_VALUE_SUCCESSED,
            null);
    }

    /**
     * @param <T>
     * @param code
     * @param msg
     * @return
     */
    public static <T> BaseResponseVo<T> ok(Integer code, String msg) {
        return BaseResponseVo.ok(code, msg, null);
    }

    /**
     * @param <T>
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static <T> BaseResponseVo<T> ok(Integer code, String msg, T data) {
        return new BaseResponseVo<T>(true, code, msg, data) {
            /**
             * serialVersionUID
             */
            private static final long serialVersionUID = -3547503182168659517L;
        };
    }

    /**
     * @param <T>
     * @param data
     * @return
     */
    public static <T> BaseResponseVo<T> ok(T data) {
        return BaseResponseVo.ok(BaseConstants.RETURN_CODE_VALUE_SUCCESSED, BaseConstants.RETURN_MESSAGE_VALUE_SUCCESSED,
            data);
    }

    private Integer code;

    private String message;

    private boolean success = Boolean.TRUE;

    private T data;

    /**
     * 
     */
    public BaseResponseVo() {}

    /**
     * @param success
     * @param code
     * @param message
     */
    public BaseResponseVo(boolean success, Integer code, String message) {
        this(success, code, message, null);
    }

    /**
     * @param success
     * @param code
     * @param message
     * @param data
     */
    public BaseResponseVo(boolean success, Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }

    /**
     * @param data
     */
    public BaseResponseVo(T data) {
        this.data = data;
    }



    /**
     * @return the code
     */
    public Integer getCode() {
        return this.code;
    }

    /**
     * @return the data
     */
    public T getData() {
        return this.data;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return this.message;
    }

    /**
     * @return the success
     */
    public boolean isSuccess() {
        return this.success;
    }

    /**
     * @param code the code to set
     */
    public BaseResponseVo<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    /**
     * @param data the data to set
     */
    public BaseResponseVo<T> setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * @param message the message to set
     */
    public BaseResponseVo<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    /**
     * @param success the success to set
     */
    public BaseResponseVo<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    /* (non-Javadoc)
     * @see com.anjvision.common.vo.BaseVo#toString()
     */
    @Override
    public String toString() {
        return JsonKit.toJson(this);
    }
}
